/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pardal.core;

import java.util.ArrayList;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.Utils;

/**
 *
 * @author Valdigleis
 */
public class ManipulationHMLP extends ArffManipulationBase{
    
    public ManipulationHMLP(Instances instances) {
        super(instances);
    }
    
    public Instances buildInstancesForLevel(int level){
        
        //Lista para os labels do nivel
        ArrayList<String> labels = new ArrayList<>();
        //Instâncias de retorno
        Instances dataHMLP = new Instances(getInstances());
        //Pega todos os nodos
        ArrayList<Node> nodesHMLP = getNodes();
        for (int i = 0; i < nodesHMLP.size(); i++) {
            if(nodesHMLP.get(i).getLevel() == level){
                String newLabel = nodesHMLP.get(i).getLabel();
                labels.add(newLabel);
            }
        }
        String classe;
        boolean test;
        for (int i = 0; i < dataHMLP.numInstances(); i++) {
            classe = dataHMLP.instance(i).stringValue(dataHMLP.classIndex());
            test = true;
            for (int j = 0; j < labels.size(); j++) {
                if(classe.contains(labels.get(j)) || classe.equals(labels.get(j))){
                    dataHMLP.instance(i).setValue(dataHMLP.classIndex(), labels.get(j));
                    test = false;
                    break;
                }
            }
            if(test){
                dataHMLP.instance(i).setValue(dataHMLP.classIndex(), Utils.missingValue());
            }
        }
        return dataHMLP;
    }
    
    /**
     * Retorna o pai de um determinado nó.
     * 
     * @param node - Node que terá seu pai checado.
     * @return - Retorna o valor do label do pai do Node caso este tenha um pai ou
     * returna null se este não possuir pai.
     */
    public String getFather(String node){
        for (int i = 0; i < this.getNodes().size(); i++) {
            if(this.getNodes().get(i).getLabel().equals(node)){
                return this.getNodes().get(i).getFather();
            }
        }
        return null;
    }
    
}
